package swu.edu.cn.javaee.controller;

import jakarta.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import swu.edu.cn.javaee.entity.Article;
import swu.edu.cn.javaee.entity.ArticleInfo;
import swu.edu.cn.javaee.service.ArticleService;
import java.util.List;
import static swu.edu.cn.javaee.component.Signal.*;


@Controller
//解决跨域问题：允许所有源域名访问
@CrossOrigin(origins = "*")

public class ArticleController {

    @Resource
    private ArticleService articleService;

    //用户创建文章信息
    @RequestMapping("/saveArticle")
    @ResponseBody
    public String saveArticle(String title,String tag,String author,String content,String description){
        String result=articleService.saveArticle(title,tag,author,content,description);
        if (result==SUCCESS_CODE){
            return result;
        }
        else {
            return FAILED_CODE;
        }
    }


    //展示所有文章信息
    @RequestMapping("/queryArticleList")
    @ResponseBody
    public ArticleInfo queryList(){
        ArticleInfo articleInfo=articleService.queryList();
        if(articleInfo!=null){
            System.out.println(articleInfo);
            return articleInfo;
        }
        else {
            return null;
        }

    }
    //分页查询请求
    @RequestMapping("/ArticleLimitInformation")
    @ResponseBody
    public ArticleInfo queryListLimit(String page,String limit){
        ArticleInfo articleInfo=articleService.queryListLimit(page,limit);
        return articleInfo;
    }

    //根据文章标题查找
    @RequestMapping("/queryArticleByTitle")
    @ResponseBody
    public List<Article> queryArticleByTitle(String title){
        List<Article> articleList=articleService.queryArticleByTitle(title);
        if (articleList!=null){
           /* System.out.println(articleList);*/
            return articleList;
        }
        else {
            return null;
        }
    }

    //根据作者查找
    @RequestMapping("/queryArticleByAuthor")
    @ResponseBody
    public List<Article> queryArticleByAuthor(String author){
        List<Article> articleList=articleService.queryArticleByAuthor(author);
        if (articleList!=null){
            return articleList;
        }
        else {
            return null;
        }

    }

    //根据类型查找
    @RequestMapping("/queryArticleByTag")
    @ResponseBody
    public List<Article> queryArticleByTag(String tag){
        List<Article> articleList=articleService.queryArticleByTag(tag);
        if (articleList!=null){
            /*System.out.println(articleList);*/
            return articleList;
        }
        else {
            return null;
        }
    }

    //用户修改文章信息
    @RequestMapping("/updateArticleByUser")
    @ResponseBody
    public String updateArticleByUser(Article article){
        String result=articleService.updateArticleByUser(article);
        if (result==SUCCESS_CODE){
            return result;
        }
        else {
            return FAILED_CODE_Article_NO_EXIST;
        }
    }
    //管理员修改信息
    @RequestMapping("/updateArticleByAdmin")
    @ResponseBody
    public String updateArticleByAdmin(String new_id,String audit_flag,String auditor,String opinion,String publish_time,String publish_not){
        String result=articleService.updateArticleByAdmin(new_id,audit_flag,auditor,opinion,publish_time,publish_not);
        if (result==SUCCESS_CODE){
            return result;
        }
        else {
            return FAILED_CODE_Article_NO_EXIST;
        }
    }

    //删除文章
    @RequestMapping("/deleteArticleById")
    @ResponseBody
    public String deleteArticleById(String new_id){
        String result= articleService.deleteArticleById(new_id);
        return result;
    }

}
